home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / TE32K.sit / TE32K Demo Folder / TE32K Source ƒ / TE32K.c < prev    next >
Text File  |  1993-01-02  |  70KB  |  2,795 lines

  1. /* PROBLEM WITH PLACEMENT OF CURSOR AT ENDS/START OF WORD-WRAPPED LINES! */
  2.  
  3. #include    "TE32K.h"
  4.  
  5. #define    EXTRALINESTARTS        32L
  6. #define    EXTRATEXTBUFF        256L
  7.  
  8. #define    LEFTARROW            28
  9. #define    RIGHTARROW            29
  10. #define UPARROW                30
  11. #define DOWNARROW            31
  12. #define    TAB                    '\t'
  13. #define DELETE                8
  14. #define    RETURN                13
  15. #define    ENTER                3
  16.  
  17.  
  18.  
  19. Handle        TE32KScrpHandle = 0L;
  20. long        TE32KScrpLength = 0L;
  21.  
  22.  
  23. static long        LineEndIndex(long,TE32KHandle);
  24. static void        CalParagraph(long,TE32KHandle,long *,long *);
  25. static long     paraLines(long,TE32KHandle);
  26. static void     updateLine(long,TE32KHandle,int,LongRect *);
  27. static void        TE32KCalTextFromLine(long,TE32KHandle);
  28. static void        TE32KCalTextFromTo(long,long,int,TE32KHandle);
  29. static void        TE32KInvertSelRange(long,long,TE32KHandle);
  30. static void        TE32KXorCaret(TE32KHandle);
  31. static long        TE32KIndexToLine(long,TE32KHandle);
  32. static int        ShiftKey(void);
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. void    TE32KSetFontStuff(int txFont,int txFace,int txMode,int txSize,TE32KHandle theTE32KHandle)
  40. {
  41. register int        i;
  42. int                    oldFont,oldFace,oldSize,oldMode;
  43. GrafPtr                oldPort;
  44. FontInfo            theFontInfo;
  45.  
  46.  
  47.     (**theTE32KHandle).txFont = txFont;
  48.     (**theTE32KHandle).txFace = txFace;
  49.     (**theTE32KHandle).txMode = txMode;
  50.     (**theTE32KHandle).txSize = txSize;
  51.     
  52.     GetPort(&oldPort);
  53.     SetPort((**theTE32KHandle).inPort);
  54.     oldFont = ((**theTE32KHandle).inPort)->txFont;
  55.     oldFace = ((**theTE32KHandle).inPort)->txFace;
  56.     oldSize = ((**theTE32KHandle).inPort)->txSize;
  57.     oldMode = ((**theTE32KHandle).inPort)->txMode;
  58.     
  59.     TextFont((**theTE32KHandle).txFont);
  60.     TextFace((**theTE32KHandle).txFace);
  61.     TextSize((**theTE32KHandle).txSize);
  62.     TextMode((**theTE32KHandle).txMode);
  63.     
  64.     
  65.     for (i=0;i<256;i++)
  66.         (**theTE32KHandle).theCharWidths[i] = CharWidth((unsigned char) i);
  67.     
  68.     GetFontInfo(&theFontInfo);
  69.     
  70.     (**theTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  71.     (**theTE32KHandle).fontAscent = theFontInfo.ascent;
  72.     
  73.     
  74.     TextFont(oldFont);
  75.     TextFace(oldFace);
  76.     TextSize(oldSize);
  77.     TextMode(oldMode);
  78.     
  79.     SetPort(oldPort);
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. void    SetLongRect(LongRect *theLongRect,long left,long top,long right,long bottom)
  87. {
  88.     theLongRect->left = left;
  89.     theLongRect->top = top;
  90.     theLongRect->right = right;
  91.     theLongRect->bottom = bottom;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. void    RectToLongRect(Rect *theRect,LongRect *theLongRect)
  99. {
  100.     theLongRect->left = (long) theRect->left;
  101.     theLongRect->top = (long) theRect->top;
  102.     theLongRect->right = (long) theRect->right;
  103.     theLongRect->bottom = (long) theRect->bottom;
  104. }
  105.  
  106.  
  107.  
  108.  
  109. void    LongRectToRect(LongRect *theLongRect,Rect *theRect)
  110. {
  111.     if (theLongRect->left < -32768L)
  112.         theRect->left = (int) -32768;
  113.     else if (theLongRect->left > 32767L)
  114.         theRect->left = (int) 32767;
  115.     else
  116.         theRect->left = (int) theLongRect->left;
  117.     
  118.     if (theLongRect->top < -32768L)
  119.         theRect->top = (int) -32768;
  120.     else if (theLongRect->top > 32767L)
  121.         theRect->top = (int) 32767;
  122.     else
  123.         theRect->top = (int) theLongRect->top;
  124.     
  125.     if (theLongRect->right < -32768L)
  126.         theRect->right = (int) -32768;
  127.     else if (theLongRect->right > 32767L)
  128.         theRect->right = (int) 32767;
  129.     else
  130.         theRect->right = (int) theLongRect->right;
  131.     
  132.     if (theLongRect->bottom < -32768L)
  133.         theRect->bottom = (int) -32768;
  134.     else if (theLongRect->bottom > 32767L)
  135.         theRect->bottom = (int) 32767;
  136.     else
  137.         theRect->bottom = (int) theLongRect->bottom;
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. static long    TE32KIndexToLine(long selIndex,TE32KHandle theTE32KHandle)
  146. {
  147. register long    i,delta;
  148.  
  149.     if (theTE32KHandle)
  150.     {
  151.         if (selIndex<=0L || (**theTE32KHandle).nLines<=1L || (**theTE32KHandle).teLength<1L)
  152.             return(0L);
  153.         
  154.         else if (selIndex >= (**theTE32KHandle).teLength)
  155.             return((long) ((**theTE32KHandle).nLines - 1L));
  156.         
  157.         else
  158.         {
  159.             i = ((**theTE32KHandle).nLines) >> 1;
  160.             
  161.             delta = ((**theTE32KHandle).nLines) >> 1;
  162.             if (delta < 1L)
  163.                 delta = 1L;
  164.             
  165.             while (delta > 0L)
  166.             {
  167.                 if (selIndex == (**theTE32KHandle).lineStarts[i])
  168.                     delta = 0L;
  169.                 
  170.                 else if (selIndex > (**theTE32KHandle).lineStarts[i])
  171.                 {
  172.                     if (selIndex < (**theTE32KHandle).lineStarts[i+1])
  173.                         delta = 0L;
  174.                     
  175.                     else
  176.                         i += delta;
  177.                 }
  178.                 
  179.                 else
  180.                     i -= delta;
  181.                 
  182.                 if (delta)
  183.                 {
  184.                     delta >>= 1;
  185.                     
  186.                     if (delta < 1L)
  187.                         delta = 1L;
  188.                 }
  189.             }
  190.         }
  191.         
  192.         if (i < 0L)
  193.             i = 0L;
  194.         else if (i >= (**theTE32KHandle).nLines)
  195.             i = (**theTE32KHandle).nLines - 1L;
  196.         
  197.         return((long) i);
  198.     }
  199.     
  200.     else
  201.         return((long) 0L);
  202. }
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. void    TE32KXorCaret(TE32KHandle theTE32KHandle)
  211. {
  212. GrafPtr            oldPort;
  213. PenState        oldPenState;
  214. Point            selPt;
  215. RgnHandle        oldClipRgn;
  216. Rect            theClipRect;
  217.  
  218.     if (theTE32KHandle && (**theTE32KHandle).active && (**theTE32KHandle).selStart==(**theTE32KHandle).selEnd)
  219.     {
  220.         if (!(**theTE32KHandle).caretState && ((**theTE32KHandle).selStart < 0 || (**theTE32KHandle).selEnd > (**theTE32KHandle).teLength))
  221.             return;
  222.         
  223.         GetPort(&oldPort);
  224.         SetPort((**theTE32KHandle).inPort);
  225.         
  226.         GetPenState(&oldPenState);
  227.         oldClipRgn = NewRgn();
  228.         GetClip(oldClipRgn);
  229.         
  230.         theClipRect.left = (int) ((**theTE32KHandle).viewRect.left);
  231.         theClipRect.top = (int) ((**theTE32KHandle).viewRect.top);
  232.         theClipRect.right = (int) ((**theTE32KHandle).viewRect.right);
  233.         theClipRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  234.         
  235.         ClipRect(&theClipRect);
  236.         
  237.         PenNormal();
  238.         
  239.         PenMode(patXor);
  240.         
  241.         if ((**theTE32KHandle).selPoint.h < -32768L)
  242.             selPt.h = (int) -32768;
  243.         else if ((**theTE32KHandle).selPoint.h > 32767L)
  244.             selPt.h = (int) 32767;
  245.         else
  246.             selPt.h = (int) (**theTE32KHandle).selPoint.h;
  247.         
  248.         if ((**theTE32KHandle).selPoint.v < -32768L)
  249.             selPt.v = (int) -32768;
  250.         else if ((**theTE32KHandle).selPoint.v > 32767L)
  251.             selPt.v = (int) 32767;
  252.         else
  253.             selPt.v = (int) (**theTE32KHandle).selPoint.v;
  254.         
  255.         MoveTo(selPt.h - 1,selPt.v);
  256.         Line(0,-(**theTE32KHandle).fontAscent);
  257.         
  258.         (**theTE32KHandle).caretTime = TickCount() + GetCaretTime();
  259.         (**theTE32KHandle).caretState = !(**theTE32KHandle).caretState;
  260.         
  261.         SetClip(oldClipRgn);
  262.         DisposeRgn(oldClipRgn);
  263.         
  264.         SetPenState(&oldPenState);
  265.         SetPort(oldPort);
  266.     }
  267. }
  268.  
  269.  
  270.  
  271.  
  272. void    TE32KInit()
  273. {
  274.     TE32KScrpHandle = NewHandle(0L);
  275.     TE32KScrpLength = 0L;
  276. }
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. TE32KHandle        TE32KNew(LongRect *destRect,LongRect *viewRect)
  284. {
  285. TE32KHandle        newTE32KHandle;
  286. Handle            hText;
  287. GrafPtr            activePort;
  288. FontInfo        theFontInfo;
  289. LongPoint        selPt;
  290. int                i;
  291.  
  292.     newTE32KHandle = (TE32KHandle) NewHandle((long) sizeof(TE32KRec) + (long) sizeof(long)*EXTRALINESTARTS);
  293.     if (MemError() || StripAddress(newTE32KHandle)==0L)
  294.         return((TE32KHandle) 0L);
  295.     
  296.     hText = NewHandle(EXTRATEXTBUFF);
  297.     if (MemError() || StripAddress(hText)==0L)
  298.     {
  299.         DisposHandle((Handle) newTE32KHandle);
  300.         return((TE32KHandle) 0L);
  301.     }
  302.     
  303.     (**newTE32KHandle).destRect = *destRect;
  304.     (**newTE32KHandle).viewRect = *viewRect;
  305.     
  306.     GetPort(&activePort);
  307.     GetFontInfo(&theFontInfo);
  308.     
  309.     (**newTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  310.     (**newTE32KHandle).fontAscent = theFontInfo.ascent;
  311.     
  312.     (**newTE32KHandle).selStart = 0L;
  313.     (**newTE32KHandle).selEnd = 0L;
  314.     
  315.     (**newTE32KHandle).teLength = 0L;
  316.     (**newTE32KHandle).hText = hText;
  317.     
  318.     ((char *) *(**newTE32KHandle).hText)[(**newTE32KHandle).teLength] = '\r';
  319.     
  320.     (**newTE32KHandle).txFont = activePort->txFont;
  321.     (**newTE32KHandle).txFace = activePort->txFace;
  322.     (**newTE32KHandle).txMode = activePort->txMode;
  323.     (**newTE32KHandle).txSize = activePort->txSize;
  324.     
  325.     (**newTE32KHandle).inPort = activePort;
  326.     
  327.     (**newTE32KHandle).tabWidth = 25;
  328.     
  329.     (**newTE32KHandle).crOnly = 0x00;
  330.     
  331.     (**newTE32KHandle).nLines = 1L;
  332.     (**newTE32KHandle).lineStarts[0] = 0L;
  333.     (**newTE32KHandle).lineStarts[1] = 0L;
  334.     
  335.     (**newTE32KHandle).active = TRUE;
  336.     (**newTE32KHandle).caretState = FALSE;
  337.     (**newTE32KHandle).caretTime = TickCount();
  338.     
  339.     (**newTE32KHandle).clickTime = TickCount();
  340.     (**newTE32KHandle).clickLoc = -1L;
  341.     
  342.     TE32KGetPoint((**newTE32KHandle).selStart,&selPt,newTE32KHandle);
  343.     
  344.     (**newTE32KHandle).selPoint = selPt;
  345.     
  346.     (**newTE32KHandle).clikLoop = 0L;
  347.     
  348.     TE32KSetFontStuff((**newTE32KHandle).txFont,(**newTE32KHandle).txFace,(**newTE32KHandle).txMode,(**newTE32KHandle).txSize,newTE32KHandle);
  349.     
  350.     return((TE32KHandle) newTE32KHandle);
  351. }
  352.  
  353.  
  354.  
  355.  
  356.  
  357. void TE32KDispose(TE32KHandle theTE32KHandle)
  358. {
  359.     if (theTE32KHandle)
  360.     {
  361.         if ((**theTE32KHandle).hText)
  362.             DisposHandle((**theTE32KHandle).hText);
  363.         
  364.         DisposHandle(theTE32KHandle);
  365.     }
  366. }
  367.  
  368.  
  369.  
  370.  
  371. void    TE32KCalTextFromTo(long firstLine,long lastLine,int allLines,TE32KHandle theTE32KHandle)
  372. {
  373. register unsigned char    *charPtr,ch;
  374. register long            nLines,charCount;
  375. register int            *theCharWidths;
  376. long                    maxLineStarts,sizeTE32KHandle;
  377. unsigned char            *charBase;
  378. Point                    cursorPt;
  379. int                        rightSide,destLeftSide,tabWidth,maxRewind;
  380. unsigned char            *oldCharPtr;
  381. long                    oldCharCount,tempOffset;
  382.  
  383.     if (allLines)
  384.     {
  385.         firstLine = 0L;
  386.         lastLine = (**theTE32KHandle).nLines;
  387.     }
  388.     
  389.     if (theTE32KHandle && firstLine <= (**theTE32KHandle).nLines && firstLine >= 0L && lastLine <= (**theTE32KHandle).nLines && lastLine >= 0L)
  390.     {
  391.         if (firstLine > lastLine)
  392.         {
  393.             nLines = firstLine;
  394.             firstLine = lastLine;
  395.             lastLine = nLines;
  396.         }
  397.         
  398.         (**theTE32KHandle).lineStarts[0] = 0L;
  399.         ((char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).teLength] = '\r';
  400.         
  401.         sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  402.         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  403.         
  404.         nLines = firstLine;
  405.         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  406.         charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  407.         if (allLines)
  408.             charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  409.         else
  410.             charCount = (**theTE32KHandle).lineStarts[lastLine] - (**theTE32KHandle).lineStarts[firstLine];
  411.         
  412.         if (charCount > (**theTE32KHandle).teLength)
  413.             charCount = (**theTE32KHandle).teLength;
  414.         
  415.         
  416.         if (charCount)
  417.         {
  418.             rightSide = (int) ((**theTE32KHandle).destRect.right);
  419.             destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  420.             cursorPt.h = destLeftSide;
  421.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  422.             
  423.             theCharWidths = (**theTE32KHandle).theCharWidths;
  424.             
  425.             while (charCount--)
  426.             {
  427.                 ch = *charPtr++;
  428.                 
  429.                 if (!(**theTE32KHandle).crOnly)
  430.                 {
  431.                     if (ch == TAB)
  432.                         cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  433.                     else if (ch != '\r')
  434.                         cursorPt.h += theCharWidths[ch];
  435.                 }
  436.                 
  437.                 if (ch == '\r' || (ch != ' ' && cursorPt.h >= rightSide))
  438.                 {
  439.                     if (ch != ' ' && cursorPt.h >= rightSide)
  440.                     {
  441.                         maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[nLines];
  442.                         oldCharPtr = charPtr;
  443.                         oldCharCount = charCount;
  444.                         
  445.                         charPtr--;
  446.                         charCount++;
  447.                         maxRewind--;
  448.                         
  449.                         while (*charPtr != ' ' && maxRewind > 0)
  450.                         {
  451.                             charPtr--;
  452.                             charCount++;
  453.                             maxRewind--;
  454.                         }
  455.                         
  456.                         if (maxRewind <= 0)
  457.                         {
  458.                             charPtr = oldCharPtr;
  459.                             charCount = oldCharCount;
  460.                         }
  461.                         
  462.                         else
  463.                         {
  464.                             charPtr++;
  465.                             charCount--;
  466.                         }
  467.                     }
  468.                     
  469.                     if (nLines >= maxLineStarts)
  470.                     {
  471.                         tempOffset = charPtr - charBase;
  472.                                                 
  473.                         sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*(nLines + EXTRALINESTARTS);
  474.                         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  475.                         
  476.                         SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  477.                         
  478.                         if (MemError())
  479.                             return;
  480.                         
  481.                         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  482.                         charPtr = charBase + tempOffset;
  483.                         theCharWidths = (**theTE32KHandle).theCharWidths;
  484.                     }
  485.                     
  486.                     (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;
  487.                     
  488.                     cursorPt.h = destLeftSide;
  489.                 }
  490.             }
  491.             
  492.             (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;
  493.         }
  494.         
  495.         if (allLines && nLines > 1L)
  496.             (**theTE32KHandle).nLines = nLines;
  497.     }
  498. }
  499.  
  500.  
  501.  
  502.  
  503. void    TE32KCalTextFromLine(long firstLine,TE32KHandle theTE32KHandle)
  504. {
  505.     TE32KCalTextFromTo(firstLine,0,TRUE,theTE32KHandle);
  506. }
  507.  
  508.  
  509.  
  510.  
  511. void TE32KCalText(TE32KHandle theTE32KHandle)
  512. {
  513.     TE32KCalTextFromTo(0L,0L,TRUE,theTE32KHandle);
  514. }
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522. void    TE32KSetText(Ptr textPtr,long textLength,TE32KHandle theTE32KHandle)
  523. {
  524. Handle        hText;
  525. LongPoint    selPt;
  526.  
  527.     if (theTE32KHandle)
  528.     {
  529.         hText = NewHandle(textLength + EXTRATEXTBUFF);
  530.         
  531.         if (MemError() || StripAddress(hText)==0L)
  532.             return;
  533.         
  534.         if ((**theTE32KHandle).hText)
  535.             DisposHandle((**theTE32KHandle).hText);
  536.         
  537.         HLock(hText);
  538.         BlockMove(textPtr,*hText,textLength);
  539.         HUnlock(hText);
  540.         
  541.         (**theTE32KHandle).hText = hText;
  542.         (**theTE32KHandle).teLength = textLength;
  543.         
  544.         ((char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).teLength] = '\r';
  545.         
  546.         (**theTE32KHandle).selStart = textLength;
  547.         (**theTE32KHandle).selEnd = textLength;
  548.         
  549.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  550.         (**theTE32KHandle).selPoint = selPt;
  551.         
  552.         TE32KCalText(theTE32KHandle);
  553.     }
  554. }
  555.  
  556.  
  557.  
  558.  
  559. Handle    TE32KGetText(TE32KHandle theTE32KHandle)
  560. {
  561.     if (theTE32KHandle)
  562.         return((Handle) (**theTE32KHandle).hText);
  563.     else
  564.         return((Handle) 0L);
  565. }
  566.  
  567.  
  568.  
  569.  
  570. void    TE32KUpdate(LongRect *updateLongRect,TE32KHandle theTE32KHandle)
  571. {
  572. Rect                        theClipRect,viewRect,updateRect;
  573. GrafPtr                        oldPort,currentPort;
  574. RgnHandle                    oldClipRgn;
  575. register unsigned char        *textPtr;
  576. register long                firstLine,lastLine,i,thisStart,nextStart,tabWidth;
  577. Point                        cursorPt;
  578. int                            oldFont,oldFace,oldSize,oldMode;
  579. int                            rightSide,destLeftSide;
  580. LongPoint                    selPt;
  581. unsigned char                oldCaretState;
  582.  
  583.  
  584.     if (theTE32KHandle && (**theTE32KHandle).inPort)
  585.     {
  586.         viewRect.left = (int) ((**theTE32KHandle).viewRect.left);
  587.         viewRect.top = (int) ((**theTE32KHandle).viewRect.top);
  588.         viewRect.right = (int) ((**theTE32KHandle).viewRect.right);
  589.         viewRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  590.         
  591.         updateRect.left = (int) (updateLongRect->left);
  592.         updateRect.top = (int) ((**theTE32KHandle).destRect.top + ((updateLongRect->top - (**theTE32KHandle).destRect.top)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight);
  593.         updateRect.right = (int) (updateLongRect->right);
  594.         updateRect.bottom = (int) ((**theTE32KHandle).destRect.top + ((updateLongRect->bottom - (**theTE32KHandle).destRect.top + (**theTE32KHandle).lineHeight - 1L)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight);
  595.         
  596.         if (SectRect(&viewRect,&updateRect,&theClipRect))
  597.         {
  598.             GetPort(&oldPort);
  599.             currentPort = (**theTE32KHandle).inPort;
  600.             SetPort(currentPort);
  601.             
  602.             oldClipRgn = NewRgn();
  603.             GetClip(oldClipRgn);
  604.             ClipRect(&theClipRect);
  605.             
  606.             if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && (**theTE32KHandle).caretState)
  607.             {
  608.                 TE32KXorCaret(theTE32KHandle);
  609.                 oldCaretState = TRUE;
  610.             }
  611.             
  612.             else
  613.                 oldCaretState = FALSE;
  614.             
  615.             firstLine = ((long) theClipRect.top - (**theTE32KHandle).destRect.top)/(long) (**theTE32KHandle).lineHeight;
  616.             lastLine = ((long) theClipRect.bottom - (**theTE32KHandle).destRect.top - 1L)/(long) (**theTE32KHandle).lineHeight;
  617.             
  618.             if (firstLine < 0)
  619.                 firstLine = 0;
  620.             
  621.             if (lastLine >= (**theTE32KHandle).nLines)
  622.                 lastLine = (**theTE32KHandle).nLines - 1L;
  623.             
  624.             if (firstLine > lastLine)
  625.                 lastLine = firstLine;
  626.             
  627.             EraseRect(&theClipRect);
  628.             
  629.             if (firstLine < (**theTE32KHandle).nLines && (**theTE32KHandle).teLength > 0L)
  630.             {
  631.                 rightSide = theClipRect.right;
  632.                 destLeftSide = (int) (**theTE32KHandle).destRect.left + 1L;
  633.                 cursorPt.h = destLeftSide;
  634.                 cursorPt.v = (int) ((**theTE32KHandle).destRect.top + firstLine * (long) (**theTE32KHandle).lineHeight + (long) (**theTE32KHandle).fontAscent);
  635.                 
  636.                 oldFont = ((**theTE32KHandle).inPort)->txFont;
  637.                 oldFace = ((**theTE32KHandle).inPort)->txFace;
  638.                 oldSize = ((**theTE32KHandle).inPort)->txSize;
  639.                 oldMode = ((**theTE32KHandle).inPort)->txMode;
  640.                 
  641.                 TextFont((**theTE32KHandle).txFont);
  642.                 TextFace((**theTE32KHandle).txFace);
  643.                 TextSize((**theTE32KHandle).txSize);
  644.                 TextMode((**theTE32KHandle).txMode);
  645.                 
  646.                 HLock((**theTE32KHandle).hText);
  647.                 
  648.                 textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  649.                 tabWidth = (long) (**theTE32KHandle).tabWidth;
  650.                 
  651.                 while (firstLine <= lastLine)
  652.                 {
  653.                     thisStart = (**theTE32KHandle).lineStarts[firstLine];
  654.                     i = thisStart;
  655.                     
  656.                     nextStart = (**theTE32KHandle).lineStarts[firstLine+1];
  657.                     
  658.                     if (nextStart > thisStart && textPtr[nextStart-1] == '\r')
  659.                         nextStart--;
  660.                     
  661.                     MoveTo(cursorPt.h,cursorPt.v);
  662.                     
  663.                     while (thisStart < nextStart)
  664.                     {
  665.                         while (i<nextStart && textPtr[i]!=TAB)
  666.                             i++;
  667.                         
  668.                         if (i > thisStart)
  669.                             DrawText(&(textPtr[thisStart]),0,(int) (i - thisStart));
  670.                         
  671.                         if (i<nextStart && textPtr[i]==TAB)
  672.                         {
  673.                             MoveTo(destLeftSide + ((currentPort->pnLoc.h - destLeftSide + tabWidth)/tabWidth)*tabWidth,currentPort->pnLoc.v);
  674.                             i++;
  675.                         }
  676.                         
  677.                         thisStart = i;
  678.                         
  679.                         if (currentPort->pnLoc.h > theClipRect.right)
  680.                             thisStart = nextStart;
  681.                     }
  682.                     
  683.                     firstLine++;
  684.                     cursorPt.v += (**theTE32KHandle).lineHeight;
  685.                 }
  686.                 
  687.                 HUnlock((**theTE32KHandle).hText);
  688.                 
  689.                 TextFont(oldFont);
  690.                 TextFace(oldFace);
  691.                 TextSize(oldSize);
  692.                 TextMode(oldMode);
  693.             }
  694.             
  695.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  696.                 TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  697.             
  698.             else
  699.             {
  700.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  701.                 (**theTE32KHandle).selPoint = selPt;
  702.                 
  703.                 if (oldCaretState)
  704.                     TE32KXorCaret(theTE32KHandle);
  705.             }
  706.             
  707.             SetClip(oldClipRgn);
  708.             DisposeRgn(oldClipRgn);
  709.             
  710.             SetPort(oldPort);
  711.         }
  712.     }
  713. }
  714.  
  715.  
  716.  
  717.  
  718.  
  719. void    TE32KScroll(long horiz,long vert,TE32KHandle theTE32KHandle)
  720. {
  721. LongRect    updateLongRect;
  722. Rect        scrollRect;
  723. RgnHandle    updateRgn;
  724. GrafPtr        oldPort;
  725.  
  726.     if (theTE32KHandle && (**theTE32KHandle).inPort && (horiz || vert))
  727.     {
  728.         GetPort(&oldPort);
  729.         SetPort((**theTE32KHandle).inPort);
  730.         
  731.         (**theTE32KHandle).destRect.left += horiz;
  732.         (**theTE32KHandle).destRect.top += vert;
  733.         (**theTE32KHandle).destRect.right += horiz;
  734.         (**theTE32KHandle).destRect.bottom += vert;
  735.         
  736.         (**theTE32KHandle).selPoint.h += horiz;
  737.         (**theTE32KHandle).selPoint.v += vert;
  738.         
  739.         scrollRect.left = (int) ((**theTE32KHandle).viewRect.left);
  740.         scrollRect.top = (int) ((**theTE32KHandle).viewRect.top);
  741.         scrollRect.right = (int) ((**theTE32KHandle).viewRect.right);
  742.         scrollRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  743.         
  744.         if (horiz < ((**theTE32KHandle).viewRect.right-(**theTE32KHandle).viewRect.left) ||
  745.             vert < ((**theTE32KHandle).viewRect.bottom-(**theTE32KHandle).viewRect.top))
  746.         {
  747.             updateRgn = NewRgn();
  748.             
  749.             ScrollRect(&scrollRect,(int) horiz,(int) vert,updateRgn);
  750.             
  751.             updateLongRect.left = (**updateRgn).rgnBBox.left;
  752.             updateLongRect.top = (**updateRgn).rgnBBox.top;
  753.             updateLongRect.right = (**updateRgn).rgnBBox.right;
  754.             updateLongRect.bottom = (**updateRgn).rgnBBox.bottom;
  755.             
  756.             DisposeRgn(updateRgn);
  757.             
  758.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  759.         }
  760.         
  761.         else
  762.         {
  763.             updateLongRect = (**theTE32KHandle).viewRect;
  764.             
  765.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  766.         }
  767.         
  768.         SetPort(oldPort);
  769.     }
  770. }
  771.  
  772.  
  773.  
  774.  
  775.  
  776. void    TE32KActivate(TE32KHandle theTE32KHandle)
  777. {
  778. LongPoint    selPt;
  779.  
  780.     if (theTE32KHandle && !((**theTE32KHandle).active))
  781.     {
  782.         (**theTE32KHandle).active = TRUE;
  783.         (**theTE32KHandle).caretState = FALSE;
  784.         
  785.         TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  786.     }
  787. }
  788.  
  789.  
  790.  
  791.  
  792.  
  793. void    TE32KIdle(TE32KHandle theTE32KHandle)
  794. {
  795.     if (theTE32KHandle && (**theTE32KHandle).active && TickCount() >= (**theTE32KHandle).caretTime)
  796.     {
  797.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  798.             TE32KXorCaret(theTE32KHandle);
  799.     }
  800. }
  801.  
  802.  
  803.  
  804.  
  805.  
  806. void    TE32KDeactivate(TE32KHandle theTE32KHandle)
  807. {
  808.     if (theTE32KHandle && (**theTE32KHandle).active)
  809.     {
  810.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  811.         {
  812.             if ((**theTE32KHandle).caretState)
  813.                 TE32KXorCaret(theTE32KHandle);
  814.         }
  815.         else
  816.             TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  817.         
  818.         (**theTE32KHandle).active = FALSE;
  819.     }
  820. }
  821.  
  822.  
  823.  
  824. void    TE32KGetPoint(long selIndex,LongPoint *selPt,TE32KHandle theTE32KHandle)
  825. {
  826. register unsigned char    *textPtr;
  827. register int            *theCharWidths;
  828. register long            i,thisStart,tabWidth;
  829. long                    x,y,lineIndex,destLeftSide;
  830. unsigned char            ch;
  831.  
  832.     if (theTE32KHandle)
  833.     {
  834.         if (selIndex<=0L || (**theTE32KHandle).teLength<1L)
  835.         {
  836.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  837.             selPt->v = (**theTE32KHandle).destRect.top + (**theTE32KHandle).fontAscent;
  838.             
  839.             return;
  840.         }
  841.         
  842.         i = TE32KIndexToLine(selIndex,theTE32KHandle);
  843.         
  844.         y = (**theTE32KHandle).destRect.top + ((**theTE32KHandle).lineHeight * i) + (**theTE32KHandle).fontAscent;
  845.         
  846.         selPt->v = y;
  847.         
  848.         if (selIndex <= (**theTE32KHandle).lineStarts[i])
  849.         {
  850.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  851.             return;
  852.         }
  853.         
  854.         
  855.         HLock((**theTE32KHandle).hText);
  856.         
  857.         lineIndex  = i;
  858.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  859.         
  860.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  861.         x = destLeftSide;
  862.         
  863.         thisStart = (**theTE32KHandle).lineStarts[lineIndex];
  864.         
  865.         theCharWidths = (**theTE32KHandle).theCharWidths;
  866.         
  867.         if (textPtr[selIndex-1] != '\r')
  868.         {
  869.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  870.             
  871.             while (thisStart < selIndex)
  872.             {
  873.                 ch = textPtr[thisStart++];
  874.                 
  875.                 if (ch == TAB)
  876.                     x = destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  877.                 
  878.                 else
  879.                     x += theCharWidths[ch];
  880.             }
  881.         }
  882.         
  883.         HUnlock((**theTE32KHandle).hText);
  884.         
  885.         selPt->h = x;
  886.     }
  887. }
  888.  
  889.  
  890.  
  891.  
  892. long    TE32KGetOffset(LongPoint *selPt,TE32KHandle theTE32KHandle)
  893. {
  894. register unsigned char    *textPtr;
  895. register int            *theCharWidths;
  896. register long            i,delta,firstChar,lastChar,tabWidth;
  897. unsigned char            done;
  898. long                    x,y,selIndex,horiz,destLeftSide;
  899.  
  900.     if (theTE32KHandle)
  901.     {
  902.         if ((**theTE32KHandle).teLength < 1L)
  903.             return(0L);
  904.         
  905.         horiz = selPt->h;
  906.         
  907.         y = selPt->v - (**theTE32KHandle).destRect.top;
  908.         
  909.         i = y / (long) (**theTE32KHandle).lineHeight;
  910.         
  911.         if (i < 0L)
  912.             return(0L);
  913.         
  914.         if (i >= (**theTE32KHandle).nLines)
  915.             return((**theTE32KHandle).teLength);
  916.         
  917.         theCharWidths = (**theTE32KHandle).theCharWidths;
  918.         
  919.         HLock((**theTE32KHandle).hText);
  920.         
  921.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  922.         
  923.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  924.         x = destLeftSide;
  925.         delta = 0L;
  926.         
  927.         firstChar = (**theTE32KHandle).lineStarts[i];
  928.         lastChar = (**theTE32KHandle).lineStarts[i+1L];
  929.         
  930.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  931.         
  932.         if (firstChar<lastChar && x+delta<horiz)
  933.         {
  934.             done = FALSE;
  935.         
  936.             while (!done)
  937.             {
  938.                 if (textPtr[firstChar] != TAB)
  939.                     delta = (long) theCharWidths[textPtr[firstChar]];
  940.                 
  941.                 else
  942.                     delta = (destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth) - x;
  943.                 
  944.                 firstChar++;
  945.                 
  946.                 if (firstChar >= lastChar)
  947.                 {    
  948.                     if (textPtr[lastChar - 1L] == '\r')
  949.                         selIndex = lastChar - 1L;
  950.                     else
  951.                         selIndex = lastChar;
  952.                     
  953.                     done = TRUE;
  954.                 }
  955.                 
  956.                 else if (x+delta >= horiz)
  957.                 {
  958.                     if (horiz >= x + (delta >> 1))
  959.                         selIndex = firstChar;
  960.                     else
  961.                         selIndex = --firstChar;
  962.                     
  963.                     done = TRUE;
  964.                 }
  965.                 
  966.                 else
  967.                     x += delta;
  968.             }
  969.         }
  970.         
  971.         else
  972.             selIndex = firstChar;
  973.  
  974.         HUnlock((**theTE32KHandle).hText);
  975.         
  976.         return(selIndex);
  977.     }
  978. }
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986. void    TE32KInvertSelRange(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  987. {
  988. long        firstLine,lastLine,selIndex;
  989. Rect        viewRect,tempRect1,tempRect2,tempRect3,theRect;
  990. LongPoint    selPt;
  991. GrafPtr        oldPort;
  992.  
  993.  
  994.     if (theTE32KHandle && (**theTE32KHandle).active)
  995.     {
  996.         if ((**theTE32KHandle).caretState)
  997.             TE32KXorCaret(theTE32KHandle);
  998.         
  999.         if (selStart == selEnd)
  1000.         {
  1001.             TE32KGetPoint(selStart,&selPt,theTE32KHandle);
  1002.             (**theTE32KHandle).selPoint = selPt;
  1003.             (**theTE32KHandle).selStart = selStart;
  1004.             (**theTE32KHandle).selEnd = selStart;
  1005.             
  1006.             TE32KXorCaret(theTE32KHandle);
  1007.             
  1008.             return;
  1009.         }
  1010.         
  1011.         
  1012.         viewRect.left = (int) ((**theTE32KHandle).viewRect.left);
  1013.         viewRect.top = (int) ((**theTE32KHandle).viewRect.top);
  1014.         viewRect.right = (int) ((**theTE32KHandle).viewRect.right);
  1015.         viewRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  1016.         
  1017.         GetPort(&oldPort);
  1018.         SetPort((**theTE32KHandle).inPort);
  1019.         
  1020.         if (selStart > selEnd)
  1021.         {
  1022.             firstLine = selStart;
  1023.             selStart = selEnd;
  1024.             selEnd = firstLine;
  1025.         }
  1026.         
  1027.         firstLine = TE32KIndexToLine(selStart,theTE32KHandle);
  1028.         lastLine = TE32KIndexToLine(selEnd,theTE32KHandle);
  1029.         
  1030.         
  1031.         TE32KGetPoint(selStart,&selPt,theTE32KHandle);
  1032.         
  1033.         selPt.v -= (**theTE32KHandle).fontAscent;
  1034.         
  1035.         if (selStart <= (**theTE32KHandle).lineStarts[firstLine])
  1036.             selPt.h--;
  1037.         
  1038.         if (selPt.h < -32768L)
  1039.             tempRect1.left = (int) -32768;
  1040.         else if (selPt.h > 32767L)
  1041.             tempRect1.left = (int) 32767;
  1042.         else
  1043.             tempRect1.left = (int) selPt.h;
  1044.         
  1045.         if (selPt.v < -32768L)
  1046.             tempRect1.top = (int) -32768;
  1047.         else if (selPt.v > 32767L)
  1048.             tempRect1.top = (int) 32767;
  1049.         else
  1050.             tempRect1.top = (int) selPt.v;
  1051.         
  1052.         
  1053.         if (firstLine != lastLine)
  1054.         {
  1055.             tempRect1.right = viewRect.right;
  1056.             tempRect1.bottom = tempRect1.top + (**theTE32KHandle).lineHeight;
  1057.         }
  1058.         
  1059.         else
  1060.         {
  1061.             TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1062.             
  1063.             selPt.v -= (**theTE32KHandle).fontAscent;
  1064.             selPt.v += (**theTE32KHandle).lineHeight;
  1065.             
  1066.             if (selPt.h < -32768L)
  1067.                 tempRect1.right = (int) -32768;
  1068.             else if (selPt.h > 32767L)
  1069.                 tempRect1.right = (int) 32767;
  1070.             else
  1071.                 tempRect1.right = (int) selPt.h;
  1072.             
  1073.             if (selPt.v < -32768L)
  1074.                 tempRect1.bottom = (int) -32768;
  1075.             else if (selPt.v > 32767L)
  1076.                 tempRect1.bottom = (int) 32767;
  1077.             else
  1078.                 tempRect1.bottom = (int) selPt.v;
  1079.         }
  1080.         
  1081.         if (SectRect(&viewRect,&tempRect1,&theRect))
  1082.             InvertRect(&theRect);
  1083.         
  1084.  
  1085.         
  1086.         if (lastLine > firstLine + 1L)
  1087.         {
  1088.             TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1089.             
  1090.             tempRect2.left = viewRect.left;
  1091.             tempRect2.top = tempRect1.bottom;
  1092.             tempRect2.right = viewRect.right;
  1093.             
  1094.             selPt.v -= (**theTE32KHandle).fontAscent;
  1095.             
  1096.             if (selPt.v < -32768L)
  1097.                 tempRect2.bottom = (int) -32768;
  1098.             else if (selPt.v > 32767L)
  1099.                 tempRect2.bottom = (int) 32767;
  1100.             else
  1101.                 tempRect2.bottom = (int) selPt.v;
  1102.             
  1103.             selPt.v += (**theTE32KHandle).fontAscent;
  1104.             
  1105.             if (SectRect(&viewRect,&tempRect2,&theRect))
  1106.                 InvertRect(&theRect);
  1107.         }
  1108.         
  1109.         if (lastLine > firstLine && selEnd > (**theTE32KHandle).lineStarts[lastLine])
  1110.         {
  1111.             if (lastLine == firstLine + 1L)
  1112.                 TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1113.             
  1114.             selPt.v -= (**theTE32KHandle).fontAscent;
  1115.             
  1116.             if (selPt.v < -32768L)
  1117.                 tempRect3.top = (int) -32768;
  1118.             else if (selPt.v > 32767L)
  1119.                 tempRect3.top = (int) 32767;
  1120.             else
  1121.                 tempRect3.top = (int) selPt.v;
  1122.             
  1123.             selPt.v += (**theTE32KHandle).lineHeight;
  1124.             
  1125.             if (selPt.v < -32768L)
  1126.                 tempRect3.bottom = (int) -32768;
  1127.             else if (selPt.v > 32767L)
  1128.                 tempRect3.bottom = (int) 32767;
  1129.             else
  1130.                 tempRect3.bottom = (int) selPt.v;
  1131.             
  1132.             
  1133.             tempRect3.left = viewRect.left;
  1134.             
  1135.             if (selPt.h < -32768L)
  1136.                 tempRect3.right = (int) -32768;
  1137.             else if (selPt.h > 32767L)
  1138.                 tempRect3.right = (int) 32767;
  1139.             else
  1140.                 tempRect3.right = (int) selPt.h;
  1141.             
  1142.             
  1143.             if (SectRect(&viewRect,&tempRect3,&theRect))
  1144.                 InvertRect(&theRect);
  1145.         }
  1146.         
  1147.         
  1148.         SetPort(oldPort);
  1149.     }
  1150. }
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156. void    TE32KClick(Point startPoint,unsigned char extend,TE32KHandle theTE32KHandle)
  1157. {
  1158. register long                selIndex,selAnchor,selLast,teLength;
  1159. register unsigned char        *textPtr;
  1160. LongPoint                    selPt;
  1161. Point                        mousePt;
  1162. GrafPtr                        oldPort;
  1163. unsigned char                ch;
  1164. long                        oldClickLoc;
  1165.  
  1166.  
  1167.     if (theTE32KHandle && (**theTE32KHandle).active)
  1168.     {
  1169.         teLength = (**theTE32KHandle).teLength;
  1170.         
  1171.         selPt.h = (long) startPoint.h;
  1172.         selPt.v = (long) startPoint.v;
  1173.         
  1174.         selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1175.         
  1176.         oldClickLoc = (**theTE32KHandle).clickLoc;
  1177.         (**theTE32KHandle).clickLoc = selIndex;
  1178.  
  1179.         if ((**theTE32KHandle).caretState)
  1180.             TE32KXorCaret(theTE32KHandle);
  1181.         
  1182.         if (!extend && teLength > 0L && TickCount() < (**theTE32KHandle).clickTime + GetDblTime() && oldClickLoc == selIndex)
  1183.         {
  1184.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1185.                 TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1186.             
  1187.             selAnchor = selIndex;
  1188.             selIndex = selAnchor;
  1189.             textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1190.             
  1191.             while (selIndex > 0L)
  1192.             {
  1193.                 ch = textPtr[selIndex-1L];
  1194.                 
  1195.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1196.                     selIndex--;
  1197.                 else
  1198.                     break;
  1199.             }
  1200.             
  1201.             if (selIndex < 0L)
  1202.                 (**theTE32KHandle).selStart =  0L;
  1203.             else
  1204.                 (**theTE32KHandle).selStart = selIndex;
  1205.             
  1206.             selIndex = selAnchor;
  1207.             teLength = (**theTE32KHandle).teLength;
  1208.             
  1209.             while (selIndex < teLength)
  1210.             {
  1211.                 ch = textPtr[selIndex];
  1212.                 
  1213.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1214.                     selIndex++;
  1215.                 else
  1216.                     break;
  1217.             }
  1218.             
  1219.             if (selIndex > teLength)
  1220.                 (**theTE32KHandle).selEnd =  teLength;
  1221.             else
  1222.                 (**theTE32KHandle).selEnd = selIndex;
  1223.             
  1224.             TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1225.         }
  1226.         
  1227.         else
  1228.         {
  1229.             if (!extend)
  1230.             {
  1231.                 if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1232.                     TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1233.                 
  1234.                 (**theTE32KHandle).selStart = selIndex;
  1235.                 (**theTE32KHandle).selEnd = selIndex;
  1236.                     
  1237.                 TE32KGetPoint(selIndex,&selPt,theTE32KHandle);
  1238.                 (**theTE32KHandle).selPoint = selPt;
  1239.                     
  1240.                 TE32KXorCaret(theTE32KHandle);
  1241.             }
  1242.                 
  1243.                 
  1244.             
  1245.             if (extend || StillDown())
  1246.             {
  1247.                 if (extend)
  1248.                 {
  1249.                     if (selIndex >= (**theTE32KHandle).selEnd)
  1250.                     {
  1251.                         selAnchor = (**theTE32KHandle).selStart;
  1252.                         selLast = (**theTE32KHandle).selEnd;
  1253.                     }
  1254.                     else
  1255.                     {
  1256.                         selAnchor = (**theTE32KHandle).selEnd;
  1257.                         selLast = (**theTE32KHandle).selStart;
  1258.                     }
  1259.                 }
  1260.                 else
  1261.                 {
  1262.                     selAnchor = selIndex;
  1263.                     selLast = selIndex;
  1264.                 }
  1265.                 
  1266.                 GetPort(&oldPort);
  1267.                 SetPort((**theTE32KHandle).inPort);
  1268.                 
  1269.                 if (extend)
  1270.                     goto DOHILITE;
  1271.                 
  1272.                 while (StillDown())
  1273.                 {
  1274.                     if (selIndex >= selAnchor)
  1275.                     {
  1276.                         (**theTE32KHandle).selStart = selAnchor;
  1277.                         (**theTE32KHandle).selEnd = selIndex;
  1278.                     }
  1279.                     else
  1280.                     {
  1281.                         (**theTE32KHandle).selStart = selIndex;
  1282.                         (**theTE32KHandle).selEnd = selAnchor;
  1283.                     }
  1284.                     
  1285.                     if ((**theTE32KHandle).clikLoop)
  1286.                         (*((**theTE32KHandle).clikLoop)) ();
  1287.                     
  1288.                     GetMouse(&mousePt);
  1289.                     
  1290.                     selPt.h = (long) mousePt.h;
  1291.                     selPt.v = (long) mousePt.v;
  1292.                     
  1293.                     selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1294.     
  1295.     DOHILITE:
  1296.                     if (selLast >= selAnchor)
  1297.                     {
  1298.                         if (selIndex > selLast)
  1299.                             TE32KInvertSelRange(selLast,selIndex,theTE32KHandle);
  1300.                         
  1301.                         else if (selIndex>=selAnchor && selIndex<selLast)
  1302.                             TE32KInvertSelRange(selIndex,selLast,theTE32KHandle);
  1303.                         
  1304.                         else if (selIndex<selAnchor)
  1305.                         {
  1306.                             TE32KInvertSelRange(selAnchor,selLast,theTE32KHandle);
  1307.                             TE32KInvertSelRange(selIndex,selAnchor,theTE32KHandle);
  1308.                         }
  1309.                     }
  1310.                     
  1311.                     else
  1312.                     {
  1313.                         if (selIndex < selLast)
  1314.                             TE32KInvertSelRange(selIndex,selLast,theTE32KHandle);
  1315.                         
  1316.                         else if (selIndex<=selAnchor && selIndex>selLast)
  1317.                             TE32KInvertSelRange(selLast,selIndex,theTE32KHandle);
  1318.                         
  1319.                         else if (selIndex>selAnchor)
  1320.                         {
  1321.                             TE32KInvertSelRange(selLast,selAnchor,theTE32KHandle);
  1322.                             TE32KInvertSelRange(selAnchor,selIndex,theTE32KHandle);
  1323.                         }
  1324.                     }
  1325.                     
  1326.                     selLast = selIndex;
  1327.                 }
  1328.                 
  1329.                 SetPort(oldPort);
  1330.                 
  1331.                 
  1332.                 if (selIndex >= selAnchor)
  1333.                 {
  1334.                     (**theTE32KHandle).selStart = selAnchor;
  1335.                     (**theTE32KHandle).selEnd = selIndex;
  1336.                 }
  1337.                 else
  1338.                 {
  1339.                     (**theTE32KHandle).selStart = selIndex;
  1340.                     (**theTE32KHandle).selEnd = selAnchor;
  1341.                 }
  1342.             }
  1343.         }
  1344.         
  1345.         (**theTE32KHandle).clickTime = TickCount();
  1346.     }
  1347. }
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353. void    TE32KSetSelect(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  1354. {
  1355.     if (theTE32KHandle)
  1356.     {
  1357.         if ((**theTE32KHandle).active)
  1358.         {
  1359.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1360.                 TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1361.             
  1362.             else if ((**theTE32KHandle).caretState)
  1363.                 TE32KXorCaret(theTE32KHandle);
  1364.         }
  1365.                 
  1366.         if (selStart <= selEnd)
  1367.         {
  1368.             (**theTE32KHandle).selStart = selStart;
  1369.             (**theTE32KHandle).selEnd = selEnd;
  1370.         }
  1371.         else
  1372.         {
  1373.             (**theTE32KHandle).selStart = selEnd;
  1374.             (**theTE32KHandle).selEnd = selStart;
  1375.         }
  1376.         
  1377.         if ((**theTE32KHandle).active)
  1378.             TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1379.     }
  1380. }
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386. void    TE32KToScrap()
  1387. {
  1388.     if (TE32KScrpHandle && TE32KScrpLength > 0L)
  1389.     {
  1390.         ZeroScrap();
  1391.         
  1392.         HLock(TE32KScrpHandle);
  1393.         
  1394.         PutScrap(TE32KScrpLength,'TEXT',(Ptr) *TE32KScrpHandle);
  1395.         
  1396.         HUnlock(TE32KScrpHandle);
  1397.     }
  1398. }
  1399.  
  1400.  
  1401.  
  1402.  
  1403. void    TE32KFromScrap()
  1404. {
  1405. long    offset;
  1406.  
  1407.     if (TE32KScrpHandle && GetScrap(0L,'TEXT',&offset) > 0L)
  1408.     {
  1409.         TE32KScrpLength = GetScrap(TE32KScrpHandle,'TEXT',&offset);
  1410.     }
  1411. }
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417. void    TE32KCopy(TE32KHandle theTE32KHandle)
  1418. {
  1419.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1420.     {
  1421.         SetHandleSize(TE32KScrpHandle,(**theTE32KHandle).selEnd - (**theTE32KHandle).selStart);
  1422.         
  1423.         if (!MemError() && GetHandleSize(TE32KScrpHandle) >= ((**theTE32KHandle).selEnd - (**theTE32KHandle).selStart))
  1424.         {
  1425.             TE32KScrpLength = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1426.             
  1427.             HLock(TE32KScrpHandle);
  1428.             HLock((**theTE32KHandle).hText);
  1429.             
  1430.             BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*TE32KScrpHandle,TE32KScrpLength);
  1431.             
  1432.             HUnlock((**theTE32KHandle).hText);
  1433.             HUnlock(TE32KScrpHandle);
  1434.         }
  1435.     }
  1436. }
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442. void    TE32KCut(TE32KHandle theTE32KHandle)
  1443. {
  1444.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1445.     {
  1446.         TE32KCopy(theTE32KHandle);
  1447.         TE32KDelete(theTE32KHandle);
  1448.     }
  1449. }
  1450.  
  1451.  
  1452.  
  1453. void    TE32KDelete(TE32KHandle theTE32KHandle)
  1454. {
  1455. LongRect            updateRect;
  1456. register long        *theLine,*otherLine,theLineStart,i,delta;
  1457. long                firstLine,lastLine;
  1458. Rect                tempRect;
  1459. RgnHandle            updateRgn;
  1460.  
  1461.  
  1462.     if (theTE32KHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1463.     {
  1464.         firstLine = TE32KIndexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1465.         lastLine = TE32KIndexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  1466.         
  1467.         updateRect = (**theTE32KHandle).viewRect;
  1468.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1469.         LongRectToRect(&updateRect,&tempRect);
  1470.         
  1471.         updateRgn = NewRgn();
  1472.         ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight * (lastLine - firstLine),updateRgn);
  1473.         tempRect = (**updateRgn).rgnBBox;
  1474.         DisposeRgn(updateRgn);
  1475.         
  1476.         if ((**theTE32KHandle).selEnd != (**theTE32KHandle).teLength)
  1477.         {
  1478.             HLock((**theTE32KHandle).hText);
  1479.             BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selEnd,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1480.             HUnlock((**theTE32KHandle).hText);
  1481.         }
  1482.         
  1483.         delta = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1484.         
  1485.         theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1486.         otherLine = &((**theTE32KHandle).lineStarts[lastLine + 1L]);
  1487.         i = (**theTE32KHandle).nLines - lastLine;
  1488.         
  1489.         while (i--)
  1490.         {
  1491.             theLineStart = *(otherLine++);
  1492.             theLineStart -= delta;
  1493.             *(theLine++) = theLineStart;
  1494.         }
  1495.         
  1496.         (**theTE32KHandle).teLength -= delta;
  1497.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1498.         (**theTE32KHandle).nLines -= (lastLine - firstLine);
  1499.         
  1500.         ((char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).teLength] = '\r';
  1501.         
  1502.         RectToLongRect(&tempRect,&updateRect);
  1503.         TE32KUpdate(&updateRect,theTE32KHandle);    /* update scrolled stuff */
  1504.         
  1505.         updateLine(firstLine,theTE32KHandle,TRUE,0L);
  1506.     }
  1507. }
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513. void    TE32KInsert(Ptr textPtr,register long textLength,TE32KHandle theTE32KHandle)
  1514. {
  1515. LongRect                    updateRect;
  1516. register long                *theLine,*otherLine,i,numCRs;
  1517. long                        firstLine,teLength,maxLineStarts,sizeTE32KHandle;
  1518. register unsigned char        *charPtr,*charBase;
  1519. RgnHandle                    updateRgn;
  1520. Rect                        tempRect;
  1521.  
  1522.     if (theTE32KHandle && textPtr && textLength > 0L)
  1523.     {
  1524.         firstLine = TE32KIndexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1525.         
  1526.         teLength = (**theTE32KHandle).teLength + textLength;
  1527.         
  1528.         if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  1529.         {
  1530.             SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  1531.             
  1532.             if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength + EXTRATEXTBUFF)
  1533.                 return;
  1534.         }
  1535.         
  1536.         HLock((**theTE32KHandle).hText);
  1537.         
  1538.         if ((**theTE32KHandle).teLength - (**theTE32KHandle).selStart)
  1539.             BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + textLength,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  1540.         
  1541.         BlockMove(textPtr,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,textLength);
  1542.         
  1543.         HUnlock((**theTE32KHandle).hText);
  1544.         
  1545.         i = textLength;
  1546.         numCRs = 0L;
  1547.         charPtr = (unsigned char *) textPtr;
  1548.         
  1549.         while (i--)
  1550.         {
  1551.             if (*charPtr++ == '\r')
  1552.                 numCRs++;
  1553.         }
  1554.         
  1555.         if (numCRs)
  1556.         {
  1557.             maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1558.             
  1559.             if ((**theTE32KHandle).nLines + numCRs >= maxLineStarts)
  1560.             {
  1561.                 sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + numCRs + EXTRALINESTARTS);
  1562.                 maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1563.                 
  1564.                 SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  1565.                 
  1566.                 if (MemError())
  1567.                     return;
  1568.             }
  1569.             
  1570.             theLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  1571.             otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + numCRs]);
  1572.             i = (**theTE32KHandle).nLines - firstLine;
  1573.             
  1574.             while (i--)
  1575.                 *(otherLine--) = *(theLine--) + textLength;
  1576.             
  1577.             charPtr = (unsigned char *) (*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart);
  1578.             charBase = (unsigned char *) *((**theTE32KHandle).hText);
  1579.             theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1580.             i = numCRs;
  1581.             
  1582.             while (i--)
  1583.             {
  1584.                 while (*charPtr++ != '\r');
  1585.                 
  1586.                 *theLine++ = charPtr - charBase;
  1587.             }
  1588.             
  1589.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  1590.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1591.             
  1592.             updateRgn = NewRgn();
  1593.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * numCRs,updateRgn);
  1594.             DisposeRgn(updateRgn);
  1595.         }
  1596.         
  1597.         else
  1598.         {
  1599.             theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1600.             i = (**theTE32KHandle).nLines - firstLine;
  1601.             
  1602.             while (i--)
  1603.                 *(theLine++) += textLength;
  1604.         }
  1605.         
  1606.         
  1607.         (**theTE32KHandle).teLength = teLength;
  1608.         (**theTE32KHandle).selStart += textLength;
  1609.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1610.         (**theTE32KHandle).nLines += numCRs;
  1611.         
  1612.         ((char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).teLength] = '\r';
  1613.         
  1614.         do
  1615.         {
  1616.             updateLine(firstLine,theTE32KHandle,TRUE,0L);
  1617.             
  1618.             if (numCRs)
  1619.             {
  1620.                 theLine = (**theTE32KHandle).lineStarts;
  1621.                 charPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1622.                 
  1623.                 do
  1624.                 {
  1625.                     firstLine++;
  1626.                     
  1627.                 } while (firstLine < (**theTE32KHandle).nLines && charPtr[theLine[firstLine] - 1L] != '\r');
  1628.                 
  1629.             }
  1630.             
  1631.         } while (numCRs--);
  1632.     }
  1633. }
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640. void    TE32KPaste(TE32KHandle theTE32KHandle)
  1641. {
  1642.     if (theTE32KHandle && TE32KScrpHandle && TE32KScrpLength > 0L)
  1643.     {
  1644.         if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1645.             TE32KDelete( theTE32KHandle);
  1646.         
  1647.         HLock(TE32KScrpHandle);
  1648.         
  1649.         TE32KInsert(*TE32KScrpHandle,TE32KScrpLength,theTE32KHandle);
  1650.         
  1651.         HUnlock(TE32KScrpHandle);
  1652.     }
  1653. }
  1654.  
  1655.  
  1656.  
  1657.  
  1658. Handle    TE32KScrapHandle()
  1659. {
  1660.     return(TE32KScrpHandle);
  1661. }
  1662.  
  1663.  
  1664. long    TE32KGetScrapLen()
  1665. {
  1666.     return(TE32KScrpLength);
  1667. }
  1668.  
  1669.  
  1670. void    TE32KSetScrapLen(long newLength)
  1671. {
  1672.     TE32KScrpLength = newLength;
  1673. }
  1674.  
  1675.  
  1676.  
  1677.  
  1678. void    TE32KSelView(TE32KHandle theTE32KHandle)
  1679. {
  1680. register long    delta,screenLines,lineHeight,viewTop,viewBot,selPtV,ascent;
  1681.  
  1682.     if (theTE32KHandle && (**theTE32KHandle).active)
  1683.     {
  1684.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  1685.         {
  1686.             selPtV = (**theTE32KHandle).selPoint.v;
  1687.             viewTop = (**theTE32KHandle).viewRect.top;
  1688.             viewBot = (**theTE32KHandle).viewRect.bottom;
  1689.             lineHeight = (**theTE32KHandle).lineHeight;
  1690.             ascent = (**theTE32KHandle).fontAscent;
  1691.             
  1692.             delta = viewTop - (**theTE32KHandle).destRect.top;
  1693.             delta = delta - (delta/lineHeight)*lineHeight;
  1694.             
  1695.             if (selPtV - ascent < viewTop)
  1696.             {
  1697.                 delta += viewTop - (selPtV - ascent);
  1698.                 
  1699.                 TE32KScroll(0L,delta,theTE32KHandle);
  1700.             }
  1701.             
  1702.             else if (selPtV > viewBot)
  1703.             {
  1704.                 screenLines = (viewBot - viewTop) / lineHeight;
  1705.                 
  1706.                 delta -= (selPtV - ascent + lineHeight) - (viewTop + screenLines * lineHeight);
  1707.                 
  1708.                 TE32KScroll(0L,delta,theTE32KHandle);
  1709.             }
  1710.         }
  1711.     }
  1712. }
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718. static    DoDeleteKey(TE32KHandle theTE32KHandle)
  1719. {
  1720. Rect            tempRect;
  1721. RgnHandle        updateRgn;
  1722. int                chWidth;
  1723. long            firstLine,lineBreak;
  1724. register int    *theCharWidths;
  1725. register long    i,*lineStarts,selIndex,*otherLine;
  1726. LongRect        updateRect;
  1727. LongPoint        selPt;
  1728. unsigned char    ch,prevChar,doWrap;
  1729. long            deltaLines,numAffected;
  1730.  
  1731.     if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1732.         TE32KDelete(theTE32KHandle);
  1733.     
  1734.     else if ((**theTE32KHandle).selStart > 0L)
  1735.     {
  1736.         ch = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  1737.         
  1738.         if ((**theTE32KHandle).selStart >= 2L)
  1739.             prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 2L];
  1740.         else
  1741.             prevChar = '\0';
  1742.         
  1743.         firstLine = TE32KIndexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1744.         
  1745.         HLock((**theTE32KHandle).hText);
  1746.         BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart - 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1747.         HUnlock((**theTE32KHandle).hText);
  1748.         
  1749.         (**theTE32KHandle).teLength--;
  1750.         (**theTE32KHandle).selStart--;
  1751.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1752.         
  1753.         ((char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).teLength] = '\r';
  1754.         
  1755.         if (ch == '\r' && ((**theTE32KHandle).crOnly || (**theTE32KHandle).teLength == (**theTE32KHandle).selStart || prevChar == '\r'))
  1756.         {
  1757.             lineStarts = &((**theTE32KHandle).lineStarts[firstLine]);
  1758.             otherLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1759.             
  1760.             i = (**theTE32KHandle).nLines - firstLine;
  1761.             
  1762.             while (i--)
  1763.             {
  1764.                 selIndex = *(otherLine++);
  1765.                 *(lineStarts++) = --selIndex;
  1766.             }
  1767.             
  1768.             (**theTE32KHandle).nLines--;
  1769.             
  1770.             if (firstLine > 0L)
  1771.                 firstLine--;
  1772.             
  1773.             updateRect = (**theTE32KHandle).viewRect;
  1774.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1775.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1776.             TE32KUpdate(&updateRect,theTE32KHandle);
  1777.             
  1778.             LongRectToRect(&updateRect,&tempRect);
  1779.             tempRect.top = tempRect.bottom;
  1780.             
  1781.             if ((**theTE32KHandle).viewRect.bottom < -32768L)
  1782.                 tempRect.bottom = -32768;
  1783.             else if ((**theTE32KHandle).viewRect.bottom > 32767L)
  1784.                 tempRect.bottom = 32767;
  1785.             else
  1786.                 tempRect.bottom = (int) (**theTE32KHandle).viewRect.bottom;
  1787.             
  1788.             updateRgn = NewRgn();
  1789.             ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight,updateRgn);
  1790.             tempRect = (**updateRgn).rgnBBox;
  1791.             DisposeRgn(updateRgn);
  1792.             
  1793.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  1794.             (**theTE32KHandle).selPoint = selPt;
  1795.             
  1796.             RectToLongRect(&tempRect,&updateRect);
  1797.             
  1798.             if ((**theTE32KHandle).caretState)
  1799.                 TE32KXorCaret(theTE32KHandle);
  1800.         
  1801.             TE32KUpdate(&updateRect,theTE32KHandle);
  1802.         }
  1803.         
  1804.         else
  1805.         {
  1806.             lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  1807.             i = (**theTE32KHandle).nLines - firstLine;
  1808.             
  1809.             if (ch == '\r')
  1810.                 i++;
  1811.             
  1812.             
  1813.             while (i--)
  1814.                 (*(lineStarts--))--;
  1815.             
  1816.             theCharWidths = (**theTE32KHandle).theCharWidths;
  1817.             
  1818.             if (ch == TAB)
  1819.                 chWidth = (**theTE32KHandle).tabWidth;
  1820.             else
  1821.                 chWidth = theCharWidths[ch];
  1822.             
  1823.             if (ch == '\r')
  1824.             {
  1825.                 firstLine--;
  1826.                 
  1827.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1828.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1829.                 updateRect.left = (**theTE32KHandle).viewRect.left;
  1830.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  1831.  
  1832.             }
  1833.             
  1834.             else
  1835.             {
  1836.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1837.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1838.                 updateRect.left = (**theTE32KHandle).selPoint.h - chWidth;
  1839.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  1840.             }
  1841.             
  1842.             if ((**theTE32KHandle).caretState)
  1843.                 TE32KXorCaret(theTE32KHandle);
  1844.             
  1845.             if ((**theTE32KHandle).crOnly)
  1846.                 TE32KUpdate(&updateRect,theTE32KHandle);
  1847.             
  1848.             else
  1849.             {
  1850.                 updateLine(firstLine,theTE32KHandle,TRUE,&updateRect);
  1851.             }
  1852.         }
  1853.     }
  1854. }
  1855.  
  1856.  
  1857.  
  1858.  
  1859.  
  1860. static    DoArrowKeys(unsigned char ch,TE32KHandle theTE32KHandle)
  1861. {
  1862. LongPoint        selPt;
  1863. long            firstLine,selIndex;
  1864.  
  1865.     if (!ShiftKey())
  1866.     {
  1867.         if (ch==LEFTARROW || ch==RIGHTARROW)
  1868.         {
  1869.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1870.                 TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1871.             
  1872.             if (ch==LEFTARROW && (**theTE32KHandle).selStart > 0L)
  1873.             {
  1874.                 (**theTE32KHandle).selStart--;
  1875.                 (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1876.             }
  1877.             else if (ch==RIGHTARROW && (**theTE32KHandle).selEnd < (**theTE32KHandle).teLength)
  1878.             {
  1879.                 (**theTE32KHandle).selEnd++;
  1880.                 (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  1881.             }
  1882.             
  1883.             TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1884.         }
  1885.         
  1886.         else if (ch==UPARROW || ch==DOWNARROW)
  1887.         {
  1888.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1889.             {
  1890.                 TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1891.                 
  1892.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  1893.                 (**theTE32KHandle).selPoint = selPt;
  1894.                 (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1895.             }
  1896.                 
  1897.             firstLine = TE32KIndexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1898.             selPt = (**theTE32KHandle).selPoint;
  1899.             
  1900.             if (ch==UPARROW && firstLine > 0L)
  1901.             {
  1902.                 selPt.v -= (**theTE32KHandle).lineHeight;
  1903.                 (**theTE32KHandle).selStart = TE32KGetOffset(&selPt,theTE32KHandle);
  1904.             }
  1905.             else if (ch==DOWNARROW && firstLine < (**theTE32KHandle).nLines)
  1906.             {
  1907.                 selPt.v += (**theTE32KHandle).lineHeight;
  1908.                 (**theTE32KHandle).selStart = TE32KGetOffset(&selPt,theTE32KHandle);
  1909.             }
  1910.             
  1911.             (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1912.             
  1913.             TE32KInvertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1914.         }
  1915.     }
  1916.     
  1917.     else
  1918.     {
  1919.         if (ch==LEFTARROW)
  1920.         {
  1921.             if ((**theTE32KHandle).selStart > 0L)
  1922.             {
  1923.                 TE32KInvertSelRange((**theTE32KHandle).selStart - 1L,(**theTE32KHandle).selStart,theTE32KHandle);
  1924.                 (**theTE32KHandle).selStart--;
  1925.             }
  1926.         }
  1927.         
  1928.         else if (ch==RIGHTARROW)
  1929.         {
  1930.             if ((**theTE32KHandle).selEnd < (**theTE32KHandle).teLength)
  1931.             {
  1932.                 TE32KInvertSelRange((**theTE32KHandle).selEnd,(**theTE32KHandle).selEnd + 1L,theTE32KHandle);
  1933.                 (**theTE32KHandle).selEnd++;
  1934.             }
  1935.         }
  1936.         
  1937.         else if (ch==UPARROW)
  1938.         {
  1939.             firstLine = TE32KIndexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1940.             
  1941.             if (firstLine > 0L)
  1942.             {
  1943.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  1944.                 selPt.v -= (**theTE32KHandle).lineHeight;
  1945.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1946.                 
  1947.                 TE32KInvertSelRange(selIndex,(**theTE32KHandle).selStart,theTE32KHandle);
  1948.                 
  1949.                 (**theTE32KHandle).selStart = selIndex;
  1950.             }
  1951.         }
  1952.         
  1953.         else if (ch==DOWNARROW)
  1954.         {
  1955.             firstLine = TE32KIndexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  1956.             
  1957.             if (firstLine < (**theTE32KHandle).nLines - 1L)
  1958.             {
  1959.                 TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  1960.                 selPt.v += (**theTE32KHandle).lineHeight;
  1961.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1962.                 
  1963.                 TE32KInvertSelRange((**theTE32KHandle).selEnd,selIndex,theTE32KHandle);
  1964.                 
  1965.                 (**theTE32KHandle).selEnd = selIndex;
  1966.             }
  1967.         }
  1968.     }
  1969. }
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975. static    DoNormalChar(unsigned char ch,TE32KHandle theTE32KHandle)
  1976. {
  1977. Rect            tempRect;
  1978. RgnHandle        updateRgn;
  1979. int                chWidth,destLeftSide;
  1980. register int    *theCharWidths;
  1981. long            teLength,firstLine,lastLine,numLines,deltaLines,numAffected;
  1982. register long    i,*lineStarts,selIndex,delta,*otherLine;
  1983. LongPoint        selPt;
  1984. LongRect        updateRect;
  1985. unsigned char    prevChar,doRewrap;
  1986. GrafPtr            oldPort;
  1987. int                oldFont,oldFace,oldSize,oldMode;
  1988.     
  1989.     teLength = (**theTE32KHandle).teLength + 1L;
  1990.     
  1991.     if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  1992.     {
  1993.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  1994.         
  1995.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  1996.             return;
  1997.     }
  1998.     
  1999.     if ((**theTE32KHandle).caretState)
  2000.         TE32KXorCaret(theTE32KHandle);
  2001.     
  2002.     selPt = (**theTE32KHandle).selPoint;
  2003.     
  2004.     selPt.h--;
  2005.     
  2006.     firstLine = TE32KIndexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2007.     if ((**theTE32KHandle).selStart > 0L)
  2008.         prevChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).lineStarts[firstLine] - 1L];
  2009.     else
  2010.         prevChar = '\r';
  2011.     
  2012.     if ((**theTE32KHandle).crOnly || prevChar =='\r' || !(ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine]))
  2013.     {
  2014.         if (selPt.h < -32768L)
  2015.             tempRect.left = -32768;
  2016.         else if (selPt.h > 32767L)
  2017.             tempRect.left = 32767;
  2018.         else
  2019.             tempRect.left = (int) selPt.h;
  2020.         
  2021.         if ((**theTE32KHandle).viewRect.right < -32768L)
  2022.             tempRect.right = -32768;
  2023.         else if ((**theTE32KHandle).viewRect.right > 32767L)
  2024.             tempRect.right = 32767;
  2025.         else
  2026.             tempRect.right = (int) (**theTE32KHandle).viewRect.right;
  2027.         
  2028.         selPt.v -= (**theTE32KHandle).fontAscent;
  2029.         
  2030.         if (selPt.v < -32768L)
  2031.             tempRect.top = -32768;
  2032.         else if (selPt.v > 32767L)
  2033.             tempRect.top = 32767;
  2034.         else
  2035.             tempRect.top = (int) selPt.v;
  2036.         
  2037.         tempRect.bottom = tempRect.top + (**theTE32KHandle).lineHeight;
  2038.         
  2039.         GetPort(&oldPort);
  2040.         SetPort((**theTE32KHandle).inPort);
  2041.         
  2042.         oldFont = ((**theTE32KHandle).inPort)->txFont;
  2043.         oldFace = ((**theTE32KHandle).inPort)->txFace;
  2044.         oldSize = ((**theTE32KHandle).inPort)->txSize;
  2045.         oldMode = ((**theTE32KHandle).inPort)->txMode;
  2046.         
  2047.         TextFont((**theTE32KHandle).txFont);
  2048.         TextFace((**theTE32KHandle).txFace);
  2049.         TextSize((**theTE32KHandle).txSize);
  2050.         TextMode((**theTE32KHandle).txMode);
  2051.         
  2052.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2053.         
  2054.         if (ch == TAB)
  2055.         {
  2056.             destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  2057.             delta = (**theTE32KHandle).tabWidth;
  2058.             chWidth = (destLeftSide + ((tempRect.left + 1 - destLeftSide + delta)/delta)*delta) - (tempRect.left + 1);
  2059.         }
  2060.         else
  2061.             chWidth = theCharWidths[ch];
  2062.         
  2063.         if (tempRect.left < tempRect.right)
  2064.         {    
  2065.             updateRgn = NewRgn();
  2066.             ScrollRect(&tempRect,chWidth,0,updateRgn);
  2067.             
  2068.             if (tempRect.left+1 + chWidth > tempRect.right)
  2069.                 ClipRect(&tempRect);
  2070.             
  2071.             MoveTo(tempRect.left+1,tempRect.top + (**theTE32KHandle).fontAscent);
  2072.             if (ch != TAB)
  2073.                 DrawChar(ch);
  2074.             
  2075.             if (tempRect.left+1 + chWidth > tempRect.right)
  2076.             {
  2077.                 tempRect.left = -32768;
  2078.                 tempRect.top = -32768;
  2079.                 tempRect.right = 32767;
  2080.                 tempRect.bottom = 32767;
  2081.                 ClipRect(&tempRect);
  2082.             }
  2083.             
  2084.             DisposeRgn(updateRgn);
  2085.         }
  2086.         
  2087.         TextFont(oldFont);
  2088.         TextFace(oldFace);
  2089.         TextSize(oldSize);
  2090.         TextMode(oldMode);
  2091.         
  2092.         SetPort(oldPort);
  2093.     }
  2094.     
  2095.     HLock((**theTE32KHandle).hText);
  2096.     BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2097.     HUnlock((**theTE32KHandle).hText);
  2098.     
  2099.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = ch;
  2100.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).teLength+1L] = '\0';
  2101.     
  2102.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2103.     i = (**theTE32KHandle).nLines - firstLine;
  2104.     
  2105.     if (!(**theTE32KHandle).crOnly && prevChar != '\r' && ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2106.         i++;
  2107.     
  2108.     while (i--)
  2109.         (*(lineStarts--))++;
  2110.     
  2111.     
  2112.     (**theTE32KHandle).teLength++;
  2113.     (**theTE32KHandle).selStart++;
  2114.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2115.     (**theTE32KHandle).selPoint.h += (long) chWidth;
  2116.     
  2117.     ((char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).teLength] = '\r';
  2118.     
  2119.     if (!(**theTE32KHandle).crOnly)
  2120.         updateLine(firstLine,theTE32KHandle,FALSE,0L);
  2121.     
  2122.     TE32KXorCaret(theTE32KHandle);
  2123. }
  2124.  
  2125.  
  2126.  
  2127.  
  2128.  
  2129. static    DoReturnChar(TE32KHandle theTE32KHandle)
  2130. {
  2131. Rect            tempRect;
  2132. RgnHandle        updateRgn;
  2133. long            teLength,firstLine,lastLine,deltaLines,numAffected,tempFirstLine;
  2134. register long    i,*lineStarts,selIndex,delta,*otherLine;
  2135. LongPoint        selPt;
  2136. LongRect        updateRect;
  2137. unsigned char    prevChar,doWrap;
  2138.  
  2139.     teLength = GetHandleSize((Handle) theTE32KHandle);
  2140.     lastLine  = (teLength - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2141.     
  2142.     if ((**theTE32KHandle).nLines + 1L >= lastLine)
  2143.     {
  2144.         teLength = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + 1L + EXTRALINESTARTS);
  2145.         
  2146.         SetHandleSize((Handle) theTE32KHandle,teLength);
  2147.         
  2148.         if (MemError()  || GetHandleSize(theTE32KHandle) < teLength)
  2149.             return;
  2150.     }
  2151.     
  2152.     
  2153.     teLength = (**theTE32KHandle).teLength + 1L;
  2154.     
  2155.     if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  2156.     {
  2157.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  2158.         
  2159.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  2160.             return;
  2161.     }
  2162.     
  2163.     
  2164.     
  2165.     if ((**theTE32KHandle).selStart > 0L)
  2166.         prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  2167.     else
  2168.         prevChar = '\r';
  2169.     
  2170.     if ((**theTE32KHandle).caretState)
  2171.         TE32KXorCaret(theTE32KHandle);
  2172.     
  2173.     HLock((**theTE32KHandle).hText);
  2174.     BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2175.     HUnlock((**theTE32KHandle).hText);
  2176.     
  2177.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = RETURN;
  2178.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).teLength + 1L] = '\0';
  2179.     
  2180.     firstLine = TE32KIndexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2181.     
  2182.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2183.     otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + 1L]);
  2184.     i = (**theTE32KHandle).nLines - firstLine;
  2185.         
  2186.     while (i--)
  2187.     {
  2188.         selIndex = *(lineStarts--);
  2189.         *(otherLine--) = ++selIndex;
  2190.     
  2191.     }
  2192.     
  2193.     (**theTE32KHandle).lineStarts[firstLine + 1L] = (**theTE32KHandle).selStart + 1L;
  2194.     
  2195.     (**theTE32KHandle).nLines++;
  2196.     (**theTE32KHandle).teLength++;
  2197.     (**theTE32KHandle).selStart++;
  2198.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2199.     
  2200.     ((char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).teLength] = '\r';
  2201.     
  2202.     LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2203.     
  2204.     selPt = (**theTE32KHandle).selPoint;
  2205.     selPt.v -= (**theTE32KHandle).fontAscent;
  2206.     selPt.v += (**theTE32KHandle).lineHeight;
  2207.     
  2208.     if (selPt.v < -32768L)
  2209.         tempRect.top = -32768;
  2210.     else if (selPt.v > 32767L)
  2211.         tempRect.top = 32767;
  2212.     else
  2213.         tempRect.top = (int) selPt.v;
  2214.     
  2215.     updateRgn = NewRgn();
  2216.     ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight,updateRgn);
  2217.     DisposeRgn(updateRgn);
  2218.     
  2219.     if (!(**theTE32KHandle).crOnly)
  2220.     {
  2221.         doWrap = FALSE;
  2222.         tempFirstLine = firstLine;
  2223.         
  2224.         if (tempFirstLine > 0L && LineEndIndex(tempFirstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine])
  2225.         {
  2226.             doWrap = TRUE;
  2227.             tempFirstLine--;
  2228.         }
  2229.         
  2230.         else if (LineEndIndex(tempFirstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine + 1L])
  2231.             doWrap = TRUE;
  2232.         
  2233.         
  2234.         if (doWrap)
  2235.         {
  2236.             CalParagraph(tempFirstLine,theTE32KHandle,&deltaLines,&numAffected);
  2237.             
  2238.             if (deltaLines == 0L)
  2239.             {
  2240.                 updateRect = (**theTE32KHandle).viewRect;
  2241.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2242.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2243.             }
  2244.             
  2245.             else if (deltaLines > 0L)
  2246.             {
  2247.                 firstLine += deltaLines;
  2248.                 
  2249.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2250.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2251.                 
  2252.                 updateRgn = NewRgn();
  2253.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2254.                 DisposeRgn(updateRgn);
  2255.                 
  2256.                 updateRect = (**theTE32KHandle).viewRect;
  2257.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2258.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2259.             }
  2260.             
  2261.             else
  2262.             {
  2263.                 firstLine += deltaLines;
  2264.                 
  2265.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2266.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2267.                 
  2268.                 updateRgn = NewRgn();
  2269.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2270.                 DisposeRgn(updateRgn);
  2271.                 
  2272.                 updateRect = (**theTE32KHandle).viewRect;
  2273.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2274.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2275.             }
  2276.             
  2277.             TE32KUpdate(&updateRect,theTE32KHandle);
  2278.         }
  2279.         
  2280.         
  2281.         firstLine++;
  2282.         
  2283.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  2284.         
  2285.         if (deltaLines > 0L)
  2286.         {
  2287.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2288.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2289.             
  2290.             updateRgn = NewRgn();
  2291.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2292.             DisposeRgn(updateRgn);
  2293.         }
  2294.         
  2295.         else if (deltaLines < 0L)
  2296.         {
  2297.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2298.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2299.             
  2300.             updateRgn = NewRgn();
  2301.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2302.             DisposeRgn(updateRgn);
  2303.         }
  2304.         
  2305.         updateRect = (**theTE32KHandle).viewRect;
  2306.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L) * (**theTE32KHandle).lineHeight;
  2307.         updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * (numAffected + 1L);
  2308.         
  2309.         if ((**theTE32KHandle).caretState)
  2310.             TE32KXorCaret(theTE32KHandle);
  2311.         
  2312.         TE32KUpdate(&updateRect,theTE32KHandle);
  2313.     }
  2314.     
  2315.     else
  2316.     {
  2317.         updateRect = (**theTE32KHandle).viewRect;
  2318.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2319.         (**theTE32KHandle).selPoint = selPt;
  2320.         
  2321.         if ((**theTE32KHandle).nLines - firstLine >= 2L && (**theTE32KHandle).lineStarts[firstLine+1L]+1L < (**theTE32KHandle).lineStarts[firstLine + 2L])
  2322.         {
  2323.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2324.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight + (**theTE32KHandle).lineHeight;
  2325.             TE32KUpdate(&updateRect,theTE32KHandle);
  2326.         }
  2327.         else
  2328.         {
  2329.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2330.             (**theTE32KHandle).selPoint = selPt;
  2331.             TE32KXorCaret(theTE32KHandle);
  2332.         }
  2333.     }
  2334. }
  2335.  
  2336.  
  2337.  
  2338. static    OverTypeSelection(unsigned char ch,TE32KHandle theTE32KHandle)
  2339. {
  2340.     TE32KDelete(theTE32KHandle);
  2341.     
  2342.     if (ch==RETURN)
  2343.         DoReturnChar(theTE32KHandle);
  2344.     
  2345.     else if (ch==TAB || ch >= (unsigned char) 0x20)
  2346.         DoNormalChar(ch,theTE32KHandle);
  2347. }
  2348.  
  2349.  
  2350.  
  2351.  
  2352.  
  2353.  
  2354.  
  2355.  
  2356.  
  2357. void    TE32KKey(unsigned char ch,TE32KHandle theTE32KHandle)
  2358. {
  2359.     if (theTE32KHandle && (**theTE32KHandle).active)
  2360.     {
  2361.         ObscureCursor();
  2362.         
  2363.         if (ch == ENTER)
  2364.             ch = RETURN;
  2365.             
  2366.         
  2367.         if (ch == DELETE)
  2368.             DoDeleteKey(theTE32KHandle);
  2369.         
  2370.         else if (ch==LEFTARROW || ch==RIGHTARROW || ch==UPARROW || ch==DOWNARROW)
  2371.             DoArrowKeys(ch,theTE32KHandle);
  2372.         
  2373.         else if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd && (ch >= 0x20 || ch==TAB || ch==RETURN))
  2374.             OverTypeSelection(ch,theTE32KHandle);
  2375.         
  2376.         else if (ch==TAB || ch >= (unsigned char) 0x20)
  2377.             DoNormalChar(ch,theTE32KHandle);
  2378.         
  2379.         else if (ch==RETURN)
  2380.             DoReturnChar(theTE32KHandle);
  2381.         
  2382.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && !(**theTE32KHandle).caretState)
  2383.             TE32KXorCaret(theTE32KHandle);
  2384.     }
  2385. }
  2386.  
  2387.  
  2388.  
  2389.  
  2390.  
  2391. static long paraLines(long firstLine, TE32KHandle theTE32KHandle)
  2392. {
  2393. long                    lastLine,nLines;
  2394. register unsigned char    *charBase;
  2395. register long            *lineStarts;
  2396.  
  2397.     if ((**theTE32KHandle).crOnly)
  2398.         return(1L);
  2399.     
  2400.     lastLine = firstLine + 1L;
  2401.     nLines = (**theTE32KHandle).nLines;
  2402.     charBase = (unsigned char    *) *((**theTE32KHandle).hText);
  2403.     lineStarts = &((**theTE32KHandle).lineStarts[lastLine]);
  2404.     
  2405.     while (lastLine < nLines && charBase[*lineStarts - 1L] != '\r')
  2406.     {
  2407.         lastLine++;
  2408.         lineStarts++;
  2409.     }
  2410.     
  2411.     return(lastLine - firstLine);
  2412. }
  2413.  
  2414.  
  2415.  
  2416.  
  2417.  
  2418. static long    LineEndIndex(long firstLine,TE32KHandle theTE32KHandle)
  2419. {
  2420. register unsigned char    *charPtr,ch;
  2421. register long            charCount;
  2422. register int            *theCharWidths;
  2423. unsigned char            *charBase;
  2424. Point                    cursorPt;
  2425. int                        rightSide,destLeftSide,tabWidth;
  2426. int                        lineStatus;
  2427. unsigned char            *oldCharPtr;
  2428. long                    maxRewind;
  2429.     
  2430.     if ((**theTE32KHandle).crOnly)
  2431.         return((**theTE32KHandle).lineStarts[firstLine + 1L]);
  2432.     
  2433.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2434.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2435.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2436.     
  2437.     if (charCount > (**theTE32KHandle).teLength)
  2438.         charCount = (**theTE32KHandle).teLength;
  2439.     
  2440.     lineStatus = 0;
  2441.     
  2442.     if (charCount)
  2443.     {
  2444.         rightSide = (int) ((**theTE32KHandle).destRect.right);
  2445.         destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  2446.         cursorPt.h = destLeftSide;
  2447.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2448.         
  2449.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2450.         
  2451.         ch = ' ';
  2452.         
  2453.         while (charCount-- && ch != '\r')
  2454.         {
  2455.             ch = *charPtr++;
  2456.             
  2457.             if (ch == TAB)
  2458.                 cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2459.             else if (ch != '\r')
  2460.                 cursorPt.h += theCharWidths[ch];
  2461.             
  2462.             if (cursorPt.h >= rightSide && ch != ' ')
  2463.             {
  2464.                 maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[firstLine];
  2465.                 oldCharPtr = charPtr;
  2466.                 
  2467.                 charPtr--;
  2468.                 maxRewind--;
  2469.                 
  2470.                 while (*charPtr != ' ' && maxRewind > 0)
  2471.                 {
  2472.                     charPtr--;
  2473.                     maxRewind--;
  2474.                 }
  2475.                 
  2476.                 if (maxRewind <= 0)
  2477.                     charPtr = oldCharPtr;
  2478.                 
  2479.                 else
  2480.                     charPtr++;
  2481.                 
  2482.                 charCount = 0;
  2483.             }
  2484.         }
  2485.     }
  2486.     
  2487.     return(charPtr - charBase);
  2488. }
  2489.  
  2490.  
  2491.  
  2492. #define    NUMTEMPLINES    32
  2493.  
  2494. static void    CalParagraph(long firstLine,TE32KHandle theTE32KHandle,long *theDeltaLines,long *theNumAffected)
  2495.  
  2496. {
  2497. register unsigned char    *charPtr,ch;
  2498. register int            *theCharWidths;
  2499. register long            nLines,charCount,*lineStarts,*otherLine;
  2500. long                    maxLineStarts,sizeTE32KHandle,oldCharCount;
  2501. unsigned char            *charBase;
  2502. Point                    cursorPt;
  2503. int                        rightSide,destLeftSide,tabWidth,maxRewind;
  2504. unsigned char            *oldCharPtr;
  2505. long                    tempLineStarts[NUMTEMPLINES],oldNumLines,i,deltaLines;
  2506.     
  2507.     if ((**theTE32KHandle).crOnly)
  2508.     {
  2509.         *theDeltaLines = 0L;
  2510.         *theNumAffected = 0L;
  2511.         return;
  2512.     }
  2513.     
  2514.     deltaLines = 0L;
  2515.     
  2516.     oldNumLines = paraLines(firstLine,theTE32KHandle);
  2517.     
  2518.     for (i=0;i<oldNumLines && i <NUMTEMPLINES;i++)
  2519.         tempLineStarts[i] = (**theTE32KHandle).lineStarts[firstLine + i];
  2520.         
  2521.     sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  2522.     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2523.     
  2524.     nLines = 0;
  2525.     tempLineStarts[nLines] = (**theTE32KHandle).lineStarts[firstLine];
  2526.     
  2527.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2528.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2529.     
  2530.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2531.     ch = *charPtr;
  2532.     
  2533.     if (charCount > 0L)
  2534.     {
  2535.         rightSide = (int) ((**theTE32KHandle).destRect.right);
  2536.         destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  2537.         cursorPt.h = destLeftSide;
  2538.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2539.         
  2540.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2541.         
  2542.         ch = ' ';
  2543.         
  2544.         while (ch != '\r' && charCount--)
  2545.         {
  2546.             ch = *charPtr++;
  2547.             
  2548.             if (ch != '\r')
  2549.             {
  2550.                 if (ch == TAB)
  2551.                     cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2552.                 else
  2553.                     cursorPt.h += theCharWidths[ch];
  2554.                 
  2555.                 if (ch != ' ' && cursorPt.h >= rightSide)
  2556.                 {
  2557.                     maxRewind = charPtr - charBase - tempLineStarts[nLines];
  2558.                     oldCharPtr = charPtr;
  2559.                     oldCharCount = charCount;
  2560.                     
  2561.                     charPtr--;
  2562.                     charCount++;
  2563.                     maxRewind--;
  2564.                     
  2565.                     while (*charPtr != ' ' && maxRewind > 0)
  2566.                     {
  2567.                         charPtr--;
  2568.                         charCount++;
  2569.                         maxRewind--;
  2570.                     }
  2571.                     
  2572.                     if (maxRewind <= 0)
  2573.                     {
  2574.                         charPtr = oldCharPtr;
  2575.                         charCount = oldCharCount;
  2576.                     }
  2577.                     
  2578.                     else
  2579.                     {
  2580.                         charPtr++;
  2581.                         charCount--;
  2582.                     }
  2583.                     
  2584.                     nLines++;
  2585.                     
  2586.                     if (nLines < NUMTEMPLINES)
  2587.                     {
  2588.                         if (tempLineStarts[nLines] == charPtr - charBase)
  2589.                         {
  2590.                             oldNumLines = nLines;
  2591.                             goto STOPWRAPPING;
  2592.                         }
  2593.                         else
  2594.                             tempLineStarts[nLines] = charPtr - charBase;
  2595.                     }
  2596.                     
  2597.                     else
  2598.                         goto STOPWRAPPING;
  2599.                     
  2600.                     cursorPt.h = destLeftSide;
  2601.                 }
  2602.             }
  2603.         }
  2604.         
  2605.         nLines++;
  2606.         
  2607.         if (nLines < NUMTEMPLINES)
  2608.             tempLineStarts[nLines] = charPtr - charBase;
  2609.  
  2610. STOPWRAPPING:
  2611.  
  2612.         deltaLines = nLines - oldNumLines;
  2613.  
  2614.         if (nLines >= NUMTEMPLINES)
  2615.         {
  2616.             TE32KCalTextFromLine(firstLine,theTE32KHandle);
  2617.             deltaLines = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  2618.         }
  2619.         
  2620.         else
  2621.         {
  2622.             if (deltaLines == 0L)
  2623.             {
  2624.                 for (i = 1;i <= nLines;i++)
  2625.                     (**theTE32KHandle).lineStarts[firstLine + i] = tempLineStarts[i];
  2626.             }
  2627.             
  2628.             else if (deltaLines < 0L)
  2629.             {
  2630.                 lineStarts = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  2631.                 
  2632.                 for (i = 1;i <= nLines;i++)
  2633.                     *(lineStarts++) = tempLineStarts[i];
  2634.                 
  2635.                 otherLine = &((**theTE32KHandle).lineStarts[firstLine + oldNumLines + 1L]);
  2636.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines + 1L;
  2637.                 
  2638.                 while (i--)
  2639.                     *(lineStarts++) = *(otherLine++);
  2640.                 
  2641.                 (**theTE32KHandle).nLines += deltaLines;
  2642.             }
  2643.             
  2644.             else
  2645.             {
  2646.                 if ((**theTE32KHandle).nLines + deltaLines >= maxLineStarts)
  2647.                 {
  2648.                     sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + deltaLines + EXTRALINESTARTS);
  2649.                     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2650.                     
  2651.                     SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  2652.                     
  2653.                     if (MemError())
  2654.                     {
  2655.                         nLines = (**theTE32KHandle).nLines;
  2656.                         deltaLines = (**theTE32KHandle).nLines;
  2657.                         goto EXITPOINT;
  2658.                     }
  2659.                 }
  2660.                 
  2661.                 lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2662.                 otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + deltaLines]);
  2663.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  2664.                 
  2665.                 while (i--)
  2666.                     *(otherLine--) = *(lineStarts--);
  2667.                     
  2668.                 for (i = nLines;i >= 0;i--)
  2669.                     *(otherLine--) = tempLineStarts[i];
  2670.                 
  2671.                 (**theTE32KHandle).nLines += deltaLines;
  2672.             }
  2673.         }
  2674.     }
  2675.     
  2676. EXITPOINT:
  2677.     *theNumAffected = nLines;
  2678.     *theDeltaLines = deltaLines;
  2679. }
  2680.  
  2681.  
  2682.  
  2683.  
  2684.  
  2685. static void updateLine(register long firstLine, TE32KHandle theTE32KHandle,int doFirst, LongRect *updateClipRect)
  2686. {
  2687. Rect            tempRect;
  2688. RgnHandle        updateRgn;
  2689. LongRect        updateRect;
  2690. LongPoint        selPt;
  2691. unsigned char    doWrap;
  2692. long            deltaLines,numAffected;
  2693.  
  2694.  
  2695.     updateRect = (**theTE32KHandle).viewRect;
  2696.     updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2697.     updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  2698.     
  2699.     if (updateClipRect)
  2700.     {
  2701.         if (updateRect.top < updateClipRect->top)
  2702.             updateRect.top = updateClipRect->top;
  2703.         if (updateRect.bottom > updateClipRect->bottom)
  2704.             updateRect.bottom = updateClipRect->bottom;
  2705.         if (updateRect.left < updateClipRect->left)
  2706.             updateRect.left = updateClipRect->left;
  2707.         if (updateRect.right > updateClipRect->right)
  2708.             updateRect.right = updateClipRect->right;
  2709.     }
  2710.     
  2711.     doWrap = FALSE;
  2712.     
  2713.     if (firstLine > 0L && LineEndIndex(firstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine])
  2714.     {
  2715.         doWrap = TRUE;
  2716.         firstLine--;
  2717.     }
  2718.     
  2719.     else if (LineEndIndex(firstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine + 1L])
  2720.         doWrap = TRUE;
  2721.     
  2722.     
  2723.     if (!doWrap && doFirst)
  2724.         TE32KUpdate(&updateRect,theTE32KHandle);
  2725.     
  2726.     else if (doWrap)
  2727.     {
  2728.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  2729.         
  2730.         if (deltaLines == 0L)
  2731.         {
  2732.             updateRect = (**theTE32KHandle).viewRect;
  2733.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2734.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2735.         }
  2736.         
  2737.         else if (deltaLines > 0L)
  2738.         {
  2739.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2740.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2741.             
  2742.             updateRgn = NewRgn();
  2743.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2744.             DisposeRgn(updateRgn);
  2745.             
  2746.             updateRect = (**theTE32KHandle).viewRect;
  2747.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2748.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2749.         }
  2750.         
  2751.         else
  2752.         {
  2753.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2754.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2755.             
  2756.             updateRgn = NewRgn();
  2757.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2758.             
  2759.             updateRect.left = (**updateRgn).rgnBBox.left;
  2760.             updateRect.top = (**updateRgn).rgnBBox.top;
  2761.             updateRect.right = (**updateRgn).rgnBBox.right;
  2762.             updateRect.bottom = (**updateRgn).rgnBBox.bottom;
  2763.             
  2764.             DisposeRgn(updateRgn);
  2765.             
  2766.             TE32KUpdate(&updateRect,theTE32KHandle);
  2767.             
  2768.             updateRect = (**theTE32KHandle).viewRect;
  2769.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2770.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2771.         }
  2772.         
  2773.         TE32KUpdate(&updateRect,theTE32KHandle);
  2774.     }
  2775.  
  2776.     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2777.     (**theTE32KHandle).selPoint = selPt;
  2778. }
  2779.  
  2780.  
  2781.  
  2782.  
  2783.  
  2784.  
  2785. static int    ShiftKey()
  2786. {
  2787. char    theKeyMap[16];
  2788.  
  2789.     GetKeys(theKeyMap);
  2790.     
  2791.     if (theKeyMap[7] & 0x01)
  2792.         return(TRUE);
  2793.     else
  2794.         return(FALSE);
  2795. }